package moe.bogos.douro.api.common.soul.skill;

import moe.bogos.douro.api.common.DARegisties;
import moe.bogos.douro.api.common.event.PlayerUseSkillEvnent;
import moe.bogos.douro.api.common.network.DouroAPINetworkHandler;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;

import java.util.Random;

public class SkillHelper
{
    private static Random random = new Random();
    
    public static double getSkillRnd()
    {
        double rnd = 0.136 * random.nextGaussian() + 1.15;
        return rnd > 1.5 ? 1.5 : (rnd < 0.8 ? 0.8 : rnd);
    }
    
    public static boolean canPlayerUseSkill(PlayerEntity player, Skill skill)
    {
        return true;//TODO
    }
    
    public static int getSoulRingAgeBySkill(PlayerEntity player, String skillID)
    {
        return 0;//TODO
    }
    
    /**
     * @param player  使用技能的玩家
     * @param skillID 技能id（Registry Name）
     */
    public static void useSkill(PlayerEntity player, String skillID)
    {
        if (player.getCommandSenderWorld().isClientSide)
        {
            DouroAPINetworkHandler.useSkill(skillID);
            return;
        }
        Skill skill = DARegisties.SKILL.getValue(new ResourceLocation(skillID));
        PlayerUseSkillEvnent.Pre event = new PlayerUseSkillEvnent.Pre(player, skill);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled()) return;
        skill = event.skill;
        skillID = event.skill.getRegistryName().toString();
        if (!canPlayerUseSkill(player, skill)) return;
        int age = getSoulRingAgeBySkill(player, skillID);
        skill.onUse(player, age);
        MinecraftForge.EVENT_BUS.post(new PlayerUseSkillEvnent.Post(player, skill));
    }
}
